package com.example.practicetest.dao;

import com.example.practicetest.entity.Goods;

import java.sql.*;

/**
 * @author wushuang
 * Goods的dao层
 * 商品goods的增删查改
 */
public class GoodsDao {
    private String url = "jdbc:mysql://localhost:3306/test";
    private String username = "root";
    private String password = "root";
    private String driver = "com.mysql.cj.jdbc.Driver";
    private Connection connection = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    /**
     * 添加商品
     * @param goods
     * @return
     */
    public int addGoods(Goods goods){
        int count = 0;
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            String sql = "insert into tb_goods(name,price,stock,number) values(?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1,goods.getName());
            ps.setDouble(2,goods.getPrice());
            ps.setInt(3,goods.getStock());
            ps.setString(4,goods.getNumber());
            count = ps.executeUpdate();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 删除商品
     * @param number
     * @return
     */
    public int deleteGoods(String number){
        int count = 0;
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            String sql = "delete from tb_goods where number = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,number);
            count = ps.executeUpdate();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 更新商品
     * @param goods
     * @return
     */
    public int updateGoods(Goods goods){
        int count = 0;
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            String sql = "update tb_goods set price = ?,stock = ? where number = ?";
            ps = connection.prepareStatement(sql);
            ps.setDouble(1,goods.getPrice());
            ps.setInt(2,goods.getStock());
            ps.setString(3,goods.getNumber());
            count = ps.executeUpdate();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return count;
        }
    }

    /**
     * 查询商品
     * @param number
     * @return
     */
    public Goods findGoods(String number){
        Goods goods = null;
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            String sql = "select * from tb_goods where number = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,number);
            rs = ps.executeQuery();
            while(rs.next()){
                goods = new Goods();
                goods.setId(rs.getInt("id"));
                goods.setName(rs.getString("name"));
                goods.setPrice(rs.getDouble("price"));
                goods.setNumber(rs.getString("number"));
                goods.setStock(rs.getInt("stock"));
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return goods;
        }
    }

    /**
     * 查询商品
     * @param name
     * @return
     */
    public Goods findGoodsByName(String name){
        Goods goods = null;
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            String sql = "select * from tb_goods where name = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            while(rs.next()){
                goods = new Goods();
                goods.setId(rs.getInt("id"));
                goods.setName(rs.getString("name"));
                goods.setPrice(rs.getDouble("price"));
                goods.setNumber(rs.getString("number"));
                goods.setStock(rs.getInt("stock"));
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            releaseAll();
            return goods;
        }
    }

    /**
     * 释放资源
     */
    public void releaseAll(){
        try{
            if (ps != null){
                ps.close();
            }
            if (rs != null){
                rs.close();
            }
            if (connection != null){
                connection.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
